<template>
  <div class="app-container">
    <div class="customContainer ">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <dj-store-select :category="235" :defaultStore="formInline.store_id" @change="selectStoreChanged" />
        <el-form-item>
          <el-date-picker v-model="pickerDate" value-format="yyyy-MM" type="month" format="yyyy-MM" placeholder="选择月">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
        <!-- <el-form-item>
          <el-tooltip class="item" effect="dark" placement="bottom-end">
            <div slot="content" style="line-height:20px;width:400px">
              <br> 时间:查询的日期，日周期为今日6点至次日6点，月份周期为上一次盘点日期至本次盘点日期
              <br> 上月库存金额（元）:上个月的实际库存数所价值的价格，公式=∑（库存数*礼品成本价）；库存数为盘点后的实际库存数
              <br> 本月进货金额（元）:公式=采购单（已入库）金额+其他店调入（已入库）金额+杂项入库（已入库）金额
              <br> 本月出货金额（元）:公式=店外调出单（对方已入库）金额+礼品机出货金额+店内其他渠道出售礼品金额
              <br> 本月盘盈金额（元）:公式=∑（盘盈数*礼品成本）
              <br> 本月盘亏金额（元）:公式=∑（盘亏数*礼品成本）
              <br> 本月损益金额（元）:该时间段内，本月损益金额=本月库存金额（礼品成本价*库存数）+本月出货金额+本月盘亏金额—本月进货金额—本月盘盈金额—上月存货金额
              <br> 本月库存金额（元）:公式=∑（库存数*礼品成本价）
            </div>
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-form-item> -->
      </el-form>
      <!-- 表格 -->
      <dj-simple-table :columns="tableColumns" :data="listData" :loading="listLoading" />
    </div>
  </div>
</template>
<script>
import { getList } from '@/api/report/gift_month_form';
import { DjSimpleTable } from '@/components/table';
import { DjStoreSelect } from '@/components/store';
export default {
  components: {
    'dj-simple-table': DjSimpleTable,
    'dj-store-select': DjStoreSelect
  },
  data() {
    return {
      tableColumns: [
        {
          label: '时间',
          dtcol: 'time'
        },
        {
          label: '门店名称',
          dtcol: 'store_name'
        },
        {
          label: '上月库存金额',
          dtcol: 'last_money',
          isnum: true,
          link: 'report/store_stock_summary_form'
        },
        {
          label: '本月入库金额',
          dtcol: 'in_money',
          isnum: true,
          link: "report/stock_in_summary_form"
        },
        {
          label: '本月库存调整入库金额',
          dtcol: 'adjust_in_money',
          isnum: true,
          link:'report/stock_adjust_summary_form'
        },
        {
          label: '本月出库金额',
          dtcol: 'out_money',
          isnum: true,
          link:'report/store_outbound_summary_form'
        },
        {
          label: '本月库存调整出库金额',
          dtcol: 'adjust_out_money',
          isnum: true,
          link:'report/stock_adjust_summary_form'
        },
        {
          label: '本月盘盈金额',
          dtcol: 'profit',
          isnum: true,
          link:'report/checkCollect_form'
        },
        {
          label: '本月盘亏金额',
          dtcol: 'loss',
          isnum: true,
          link:'report/checkCollect_form'
        },
        {
          label: '本月退货金额',
          dtcol: 'return_money',
          isnum: true, 
          link: 'report/return_summary_form'
        },
        {
          label: '库存损益金额',
          dtcol: 'Inventory_amount',
          isnum: true
        },
        {
          label: '本月库存金额',
          dtcol: 'month_money',
          isnum: true,
          link: 'report/store_stock_summary_form'
        } 

      ],
      listData: [], // 对象数组元素中的对象含有store_id属性
      listLoading: false,
      formInline: {
        store_id: this.$store.getters.optionId,
        time: '',
        look_del: '0'
      },
      pickerDate: new Date(Date.UTC(new Date().getFullYear(), new Date().getMonth()))
        .toISOString()
        .slice(0, 7)
        // .replace('-', '.')
        // .replace('-', '.')
        // new Date().getFullYear() + '-' + (new Date().getMonth() + 1)
    };
  },
  // beforeRouteEnter(to, from, next) {
  //   next(vm => {
  //     //console.log("gift_month_form",1111111)
  //     if (!vm.listData || vm.listData.length == 0) {
  //       vm.searchClick();
  //       console.log(1111111,"searchClick")
  //     }
  //   });
  // },
  // created() {
  //   console.log("gift_month_form",1111111)
  //   if (!this.listData || this.listData.length == 0) {
  //     this.searchClick();
  //   }
  // },
  methods: {
    selectStoreChanged(obj) {
      for (let key in obj) this.formInline[key] = obj[key];
      this.searchClick();
    },
    // 搜索
    searchClick() {
      this.formInline.time = this.pickerDate;
      this.fetchData();
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      getList(this.formInline)
        .then(response => {
          if (!response) this.listData = [];
          else this.listData = response.data;
          let listData = this.listData 
          if(listData.length > 0) {
            for(let key in listData) {
              // 库存损益金额
              listData[key].Inventory_amount = listData[key].last_money + listData[key].in_money + listData[key].adjust_in_money - listData[key].out_money - 
              listData[key].adjust_out_money + listData[key].profit - listData[key].loss - listData[key].return_money - listData[key].month_money
            }
          }
          this.listData = listData
        // a+b+c-d-e+f-g-h-i
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
        });
    }
  }
};
</script>